package com.leji.song.mainPage;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import com.dgl.sdk.util.JsonUtil;
import com.dgl.sdk.view.advert.Advert;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.leji.song.R;
import com.leji.song.extra.BaseActivity;
import com.leji.song.extra.BaseCoreActivity;
import com.leji.song.httpInterface.BaseAjaxParams;
import com.leji.song.httpInterface.HttpInterface;
import com.leji.song.httpInterface.NomalResult;
import com.leji.song.mainPage.model.DS_Banner;
import com.leji.song.util.ActivityStackControlUtil;
import com.leji.song.util.BaiduMapUtil;
import com.leji.song.util.ConstAns;
import com.leji.song.util.MyAjaxCallBack;
import net.tsz.afinal.FinalHttp;
import org.json.JSONArray;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by colin.ding on 2014/6/4.
 */
public class MainPageActivity extends BaseActivity{

    private LinearLayout adverView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_page);
        initView();

        adverView = (LinearLayout) findViewById(R.id.adverView);

        initAdvert();

        startFindLocation();
    }

    //定位
    private void startFindLocation() {
        BaiduMapUtil baiduMapUtil = new BaiduMapUtil(this, new BaiduMapUtil.GetLocationCallback() {
            @Override
            public void onLocation(String locationString) {
                 Intent intent = new Intent();
                 intent.setAction(ConstAns.MSG_LOCATION_CHANGE);
                intent.putExtra("location", locationString);
                sendBroadcast(intent);
            }
        });

        baiduMapUtil.start();
    }

    private void initView() {

        setActivityTitle("乐急送");
    }

    private void initAdvert() {
        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetBanner.params.size, "3");

        FinalHttp fh = new FinalHttp();
        fh.get(HttpInterface.HIF_GetBanner.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity) this) {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);

                Object obj = NomalResult.doResult(o, MainPageActivity.this);
                if (obj != null) {

                    Gson gson = new Gson();
                    Type listType = new TypeToken<List<DS_Banner>>() {
                    }.getType();
                    List<DS_Banner> bannerList = gson.fromJson(gson.toJson(obj), listType);

                    ArrayList<String> advertImgList = new ArrayList<String>();
                    for (DS_Banner banner : bannerList) {
                        advertImgList.add(banner.getImgurl());
                    }

                    Advert advert = new Advert(MainPageActivity.this, advertImgList);
                    adverView.addView(advert.getView());

                }
            }
        });

    }

    public void gotoBuyFruit(View v){
        showActivity(BuyFruitActivity.class);
    }

    public void gotoBuyFood(View v){
        showActivity(BuyFoodActivity.class);
    }

    private long exitTime;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK ) {
            if((System.currentTimeMillis() - exitTime) > 2000){
                showToast("再按一次退出掌上学苑");
                exitTime = System.currentTimeMillis();
            } else {
                ActivityStackControlUtil.finishProgram();
            }
        }

        return true;
    }
}
